# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.


from odoo import api, fields, models, _

from .ocr_glority import ocr_fapiao

import time


def glority_ocr_method(fapiao):
    # print('glority_ocr_method', fapiao)
    result = ocr_fapiao(fapiao)

    for rec in result:
        one = fapiao.browse(rec['id'])
        # print('glority_ocr_method. 2.', one, rec)

        qrcode = rec['qrcode']
        company_seal = rec['company_seal']
        kind = rec['kind']
        service_name = rec['service_name']
        province = rec['province']
        city = rec['city']

        values = {
            'golrity_api_qrcode': qrcode,
            'golrity_api_company_seal': company_seal,
            'golrity_api_kind': kind,
            'golrity_api_service_name': service_name,
            'golrity_api_province': province,
            'golrity_api_city': city,
        }

        one.write(values)

    return result


class FapiaoOcrProvider(models.Model):
    _inherit = "fp.ocr.provider"

    def _request_methods(self):
        result = super()._request_methods()
        result2 = result.copy()
        result2.update({'glority': glority_ocr_method})
        return result2


class FapiaoOcr(models.Model):
    _inherit = "fp.fapiao.ocr"

    golrity_api_qrcode = fields.Char('二维码', help="")
    golrity_api_company_seal = fields.Boolean('是否有公司印章', help="")
    golrity_api_kind = fields.Char('发票消费类型', help="")
    golrity_api_service_name = fields.Char('服务类型', help="")
    golrity_api_province = fields.Char('省', help="")
    golrity_api_city = fields.Char('市', help="")
